package com.bawan.vims.common.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * <p> Description	: Zip文件解压缩工具类 </p>
 * <p> Copyright	: Copyright (c) 2015 by FITT</p>
 * <p> Author 		: JOCHEN ZHENG </p>
 * <p> Create		: 2015年3月25日 上午10:05:55 </p> 
 * <p> version 		: 1.0 </p>
 */
public class ZipFileUtil {
	private final static Logger logger = LoggerFactory.getLogger(ZipFileUtil.class);
	/**
	 * GBK编码格式
	 */
	public final static String encoding = "UTF-8";

	/**
	 * 缓冲区
	 */
	protected static byte[] buffer = new byte[512];

	/**
	 * 解压zip文件
	 * 
	 * @param unZipfileName
	 * @param targetPath
	 */
	@SuppressWarnings({ "rawtypes" })
	public static boolean unZip(String unZipfileName, String targetPath) {
		File file;
		ZipFile zipFile = null;
		int readedBytes = 0;

		OutputStream fileOutputStream = null;
		InputStream zipInputStream = null;
		OutputStream bufferedOutputStream = null;

		if (StringUtils.isBlank(unZipfileName) || StringUtils.isBlank(targetPath)) {
			return Boolean.FALSE;
		}

		try {
			zipFile = new ZipFile(unZipfileName);
			for (Enumeration entries = zipFile.getEntries(); entries.hasMoreElements();) {
				ZipEntry entry = (ZipEntry) entries.nextElement();
				String entryName = entry.getName();
				file = new File(targetPath + entryName);
				if (entry.isDirectory()) {
					file.mkdirs();
				} else {
					/**
					 * 如果指定文件的目录不存在,则创建之
					 */
					try {

						File parent = file.getParentFile();
						if (!parent.exists()) {
							parent.mkdirs();
						}

						zipInputStream = zipFile.getInputStream(entry);
						fileOutputStream = new FileOutputStream(file);
						bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

						while ((readedBytes = zipInputStream.read(buffer)) > 0) {
							bufferedOutputStream.write(buffer, 0, readedBytes);
						}
						bufferedOutputStream.flush();
					} finally {
						IOUtils.closeQuietly(bufferedOutputStream);
					}

				}
			}
		} catch (IOException e) {
			logger.error("[ZipFileUtil]" + e.getMessage(), e);
			return Boolean.FALSE;
		} finally {

			if (bufferedOutputStream != null) {
				IOUtils.closeQuietly(bufferedOutputStream);
			}

			if (fileOutputStream != null) {
				IOUtils.closeQuietly(fileOutputStream);
			}

			if (zipInputStream != null) {
				IOUtils.closeQuietly(zipInputStream);
			}

			if (zipFile != null) {
				ZipFile.closeQuietly(zipFile);
			}

		}

		return Boolean.TRUE;
	}

	/**
	 * 压缩
	 * 
	 * @param srcPathname
	 * @param zipFilepath
	 * @throws BuildException
	 * @throws RuntimeException
	 */
	public static void zip(String srcPath, String zipFileName) throws BuildException, RuntimeException {
		File file = new File(srcPath);
		if (!file.exists()) {
			throw new IllegalArgumentException("source file or directory " + srcPath + " does not exist.");
		}

		Project project = new Project();
		FileSet fileSet = new FileSet();
		fileSet.setProject(project);
		/**
		 * 判断是目录还是文件
		 */
		if (file.isDirectory()) {
			fileSet.setDir(file);

		} else {
			fileSet.setFile(file);
		}

		Zip zip = new Zip();
		zip.setProject(project);
		zip.setDestFile(new File(zipFileName));
		zip.addFileset(fileSet);
		zip.setEncoding(encoding);
		zip.execute();
	}
}
